// packageA/components/map-view-track/index.js
var amapFile = require("../../../libs/amap-wx.130 2.js"); //如：..­/..­/libs/amap-wx.js
import config from "../../../config";
const baseUrl = require('../../../api/config').dev //这里用的是ES6的写法
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: [],
    },
  },
  created() {},
  ready() {
    this.initMap();
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {},
    hide: function () {},
    resize: function () {},
  },
  observers: {},
  /**
   * 组件的初始数据
   */
  data: {
    imgBaseUrl: `${baseUrl['imgBaseUrl']}`,
    markers: [
      // {
      //   iconPath: "../../images/back.png",
      //   id: 0,
      //   latitude: 39.989643,
      //   longitude: 116.481028,
      //   width: 23,
      //   height: 33,
      // },
      // {
      //   iconPath: "../../images/back.png",
      //   id: 0,
      //   latitude: 39.90816,
      //   longitude: 116.434446,
      //   width: 24,
      //   height: 34,
      // },
    ],
    polyline: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    drivingByBicycle(myAmapFun) {
      let that = this;
      let originStr = `${this.properties.list[0].longitude},${this.properties.list[0].latitude}`
      let endStr = `${this.properties.list[this.properties.list.length - 1].longitude},${this.properties.list[this.properties.list.length - 1].latitude}`
      myAmapFun.getRidingRoute({
        origin: originStr,
        destination: endStr,
        success: function (data) {
          var points = [];
          if (data.paths && data.paths[0] && data.paths[0].rides) {
            var steps = data.paths[0].rides;
            for (var i = 0; i < steps.length; i++) {
              var poLen = steps[i].polyline.split(";");
              for (var j = 0; j < poLen.length; j++) {
                points.push({
                  longitude: parseFloat(poLen[j].split(",")[0]),
                  latitude: parseFloat(poLen[j].split(",")[1]),
                });
              }
            }
          }
          that.setData({
            polyline: [
              {
                points: points,
                color: "#0091ff",
                width: 6,
              },
            ],
          });
        },
        fail: function (info) {},
      });
    },
    drivingByCar(myAmapFun) {
      let that = this;
      myAmapFun.getDrivingRoute({
        origin: "117.117157,31.838216",
        destination: "117.187881,31.80569",
        waypoints: "117.152297,31.860708",
        success: function (data) {
          var points = [];
          if (data.paths && data.paths[0] && data.paths[0].steps) {
            var steps = data.paths[0].steps;
            for (var i = 0; i < steps.length; i++) {
              var poLen = steps[i].polyline.split(";");
              for (var j = 0; j < poLen.length; j++) {
                points.push({
                  longitude: parseFloat(poLen[j].split(",")[0]),
                  latitude: parseFloat(poLen[j].split(",")[1]),
                });
              }
            }
          }

          that.setData({
            polyline: [
              {
                points: points,
                color: "#0091ff",
                width: 6,
              },
            ],
          });
          console.log("polyline", that.data.polyline);
        },
        fail: function (info) {},
      });
    },
    drawByPointBydemo() {
      let points = this.properties.list
      let index = 0;
      let timer = setInterval(() => {
        if (index < points.length) {
          let marker = [
            {
              id: 1,
              longitude: points[index].longitude,
              latitude: points[index].latitude,
              iconPath: '../../../images/point-end.png',
              width: 30,
              height: 30,
            },
          ];
          let polyline = [
            {
              points: points.slice(0, index + 1),
              color: "#FF0000DD",
              width: 6,
              dottedLine: false,
            },
          ];
          this.setData({
            longitude: points[index].longitude,
            latitude: points[index].latitude,
            markers: marker,
            polyline: polyline,
          });
          index++;
        } else {
          clearInterval(timer);
        }
      }, 1000);
    },
    drawByPoint() {
      let points = [
        { longitude: 117.117159, latitude: 31.838462 },
        { longitude: 117.116183, latitude: 31.83847 },
        { longitude: 117.115947, latitude: 31.838486 },
        { longitude: 117.115748, latitude: 31.838523 },
        { longitude: 117.115555, latitude: 31.838555 },
        { longitude: 117.18614, latitude: 31.803327 },
        { longitude: 117.186028, latitude: 31.80344 },
        { longitude: 117.185963, latitude: 31.803526 },
        { longitude: 117.185872, latitude: 31.803778 },
      ];
      this.setData({
        polyline: [
          {
            points: points,
            color: "#0091ff",
            width: 12,
          },
        ],
      });
    },
    initMap() {
      // var key = config.gdMapKey;
      // var myAmapFun = new amapFile.AMapWX({ key });
      // 高德地图计算出驾车路线
      // this.drivingByCar(myAmapFun);
      // 高德地图计算出骑行路线
      this.drivingByBicycle(myAmapFun);
      // 根据坐标点直接连接(腾讯地图)
      // this.drawByPoint();
      // this.drawByPointBydemo();
    },
  },
});
